<template>
  <div class="ChangePassword">
    <FormPanel ref="FormPanel" :cols="1" :list="formList" formType="edit" :isSubmitBtn="false" />
    <div class="ChangePasswordBtn">
      <el-button type="primary" @click="changeHandle">修改</el-button>
      <!-- <el-button type="" @click="RestHandle">重置</el-button> -->
    </div>
  </div>
</template>

<script>
import { getUserInfoList, supereditpassword } from '@/api/common/auth/userInfo/index.js';
import { logoutJwt } from '@/api/login/index';
import store from '@/store';
import { authority } from '@/utils/authMixin';
import { mapActions, mapState } from 'vuex';
import * as types from '@/api/login/types';
import { sleep } from '@/utils';
import { testPW } from '@/utils/validate';
import { getUser, getToken, removeToken, removeUser, removeDataType, removeAppId, setToken, setDataType, setAppId, jsEncrypt } from '@/assets/js/auth';
export default {
  name: 'ChangePassword',
  data() {
    return {
      userCode: '',
      employeeName: '',
      formList: this.createFormList()
    };
  },
  created() {
    this.getUserInfo();
    this.getCookie();
  },
  computed: {
    ...mapState('app', ['btnLoading'])
  },
  methods: {
    ...mapActions('app', ['createLoginInfo']),
    getCookie() {
      let allCookie = document.cookie.split(';');
      allCookie.forEach(x => {
        if (x.indexOf('dataType') > -1) {
          this.dataType = x.split('=')[1];
        }
      });
      //判断是否为车厂10461003
      console.log(this.dataType, 'dataType');
      if (this.dataType == 10461003) {
        console.log(this.dataType, '车厂');
      }
    },
    //请求数据
    async getUserInfo() {
      await getUserInfoList()
        .then(res => {
          console.log(res.data, 'data');
          this.formList[0].initialValue = res.data.employeeNo;
          this.formList[1].initialValue = res.data.employeeName;
          this.formList[2].initialValue = res.data.userCode;
        })
        .catch();
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '员工编号',
          fieldName: 'userCode1',
          disabled: true,
          initialValue: '2',
          style: { width: '200px' }
        },
        {
          type: 'INPUT',
          label: '员工姓名',
          fieldName: 'employeeName',
          disabled: true,
          initialValue: '1',
          style: { width: '200px' }
        },
        {
          type: 'INPUT',
          label: '登陆账号',
          fieldName: 'userCode',
          disabled: true,
          initialValue: '',
          style: { width: '200px' }
        },
        {
          type: 'INPUT',
          label: '原密码',
          fieldName: 'password',
          showPassword: true,
          placeholder: '请输入原密码',
          initialValue: '',
          style: { width: '200px' },
          rules: [{ required: true, message: '请输入原密码', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '新密码',
          showPassword: true,
          fieldName: 'newPassword',
          placeholder: '请输入6-32位密码',
          initialValue: '',
          style: { width: '200px' },
          rules: [{ required: true, validator: testPW, trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '确认密码',
          fieldName: 'confirmPassword',
          showPassword: true,
          placeholder: '确认密码',
          initialValue: '',
          style: { width: '200px' },
          rules: [{ required: true, validator: testPW, trigger: 'blur' }]
        }
      ];
    },
    // 修改
    async changeHandle(val) {
      console.log(this.formList, 'this.formList');
      let data = this.$refs.FormPanel.SUBMIT_FORM();
      if (data) {
        if (this.formList[3].initialValue != this.formList[4].initialValue) {
          if (this.formList[4].initialValue != this.formList[5].initialValue) {
            this.$message.error('两次输入密码不一致');
          } else {
            const param = {
              password: this.formList[3].initialValue,
              newPassword: this.formList[4].initialValue
            };
            supereditpassword(param)
              .then(res => {
                if (res.resultCode == 200) {
                  // 需要走后台推出接口
                  logoutJwt(getToken()).then(res => {
                    //退出jwt
                    if (res.resultCode === 200) {
                      this.$message({
                        type: 'success',
                        message: '修改成功'
                      });
                      // this.createLogout();
                      store.dispatch('app/createLogout');
                      removeDataType();
                      removeAppId();
                      this.$router.push({ path: '/login' });
                    }
                  });
                }
              })
              .catch(err => {});
          }
        } else {
          this.$message.error('新旧密码不能一样');
        }
      }
    }
  }
};
</script>

<style lang="less" scoped>
.ChangePassword {
  padding: 20px;
  .ChangePasswordBtn {
    width: 100%;
    text-align: center;
  }
  .el-input {
    input[type='password']::-ms-clear {
      display: none;
    }
    input[type='password']::-ms-reveal {
      display: none;
    }
  }
}
</style>
